package cn.dong.edu.service.impl;

import cn.dong.edu.domain.dto.VideoVo;
import cn.dong.edu.mapper.VideoMapper;
import cn.dong.edu.service.VideoService;
import cn.dong.edu.domain.Video;
import cn.dong.edu.feign.VodService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author You-Dong
 * @since 2021-02-07
 */
@Service
@Slf4j
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {




    @Autowired
    private VodService vodService;

    /**
     * 查询这个章节下的所有小结（视频）
     *
     * @param chapterId
     * @return
     */
    @Override
    public List<VideoVo> findAllByChapterId(String chapterId) {
        // 结果
        List<VideoVo> result = new ArrayList<VideoVo>(10);
        // 查询
        QueryWrapper<Video> wrapper = new QueryWrapper<Video>();
        wrapper.eq("chapter_id", chapterId);
        List<Video> list = super.baseMapper.selectList(wrapper);
        // 封装
        for (Video video : list) {
            VideoVo videoVo = new VideoVo();
            BeanUtils.copyProperties(video, videoVo);
            result.add(videoVo);
        }
        return result;
    }


    /**
     * 删除视频课时
     *
     * @param videoId
     * @return
     */
    @Override
    public boolean delete(String videoId) {
        // 删除视频
        // 根据视频id查询视频源id
        Video video = super.baseMapper.selectById(videoId);
        vodService.delFile(video.getVideoSourceId());
        log.info("删除videoId ----> " + video.getChapterId());
        // 删除小结
        return super.baseMapper.deleteById(videoId) > 0;
    }
}

